package xw.qxypt.modules.socket.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xw.qxypt.common.utils.R;
import xw.qxypt.modules.baseinfo.entity.LhpRfpLogEntity;
import xw.qxypt.modules.baseinfo.service.LhpRfpLogService;
import xw.qxypt.modules.socket.service.CollectorSocketServer;
import xw.qxypt.modules.socket.util.HspUtil;
import xw.qxypt.modules.socket.util.SocketUtil;


import java.net.Socket;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("socket")
public class SocketController {
    @Autowired
    private LhpRfpLogService lhpRfpLogService;

    //发送AT指令
    @RequestMapping("/sendInstruction")
    private R sendInstruction(@RequestParam Map<String, Object> params){
        String pointCode= (String) params.get("pointCode");
        String sendMessage= (String) params.get("sendMessage");
        if (HspUtil.map.containsKey(pointCode) && HspUtil.map.get(pointCode).getSocket() != null){
            try {
                Socket ses = HspUtil.map.get(pointCode).getSocket();
                String sendHexStr = SocketUtil.str2HexStr(sendMessage);
                sendHexStr += "0D0A";
                SocketUtil.sendData(sendHexStr, ses.getOutputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return R.ok();
    }

    //接收AT指令
    @RequestMapping("/receiveInstruction")
    private R receiveInstruction(@RequestParam Map<String, Object> params){
        String pointCode= (String) params.get("pointCode");
        String receiveMessage= "";
        if (HspUtil.dtuMap.containsKey(pointCode) && HspUtil.dtuMap.get(pointCode)!= null){
            receiveMessage= HspUtil.dtuMap.get(pointCode);
            HspUtil.dtuMap.put(pointCode,null);
        }
        return R.ok().put("message",receiveMessage);
    }
    // 启动
    @RequestMapping("/startPensa/{pointCode}")
    public void startPensa(@PathVariable("pointCode") String pointCode) {
        String ningbingCode = "AB" + pointCode.substring(2);
        System.err.println(ningbingCode+"——————开始喷洒");
        // ningbingCode = "AB250203";
        /*
         * CollectorSocketServer c = new CollectorSocketServer(); c.poll2(ningbingCode);
         */
        if (HspUtil.map.containsKey(ningbingCode)
                && HspUtil.map.get(ningbingCode).getSocket() != null) {
            try {
                Socket ses = HspUtil.map.get(ningbingCode).getSocket();
                String sendMessage1 = "0000000000060005080AFF00";
                SocketUtil.sendData(sendMessage1, ses.getOutputStream());
                // 保存操作记录
                LhpRfpLogEntity rfpLog = new LhpRfpLogEntity();
                rfpLog.setPointCode(ningbingCode);
                rfpLog.setLogTime(new Date());
                rfpLog.setLogContent("发送指令，启动喷洒");
                lhpRfpLogService.save(rfpLog);
                Thread.sleep(1000);
                String sendMessage2 = "0000000000060005080A0000";
                SocketUtil.sendData(sendMessage2, ses.getOutputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 停止
    @RequestMapping("/stopPensa/{pointCode}")
    public void stopPensa(@PathVariable("pointCode") String pointCode) {
        String ningbingCode = "AB" + pointCode.substring(2);
        System.err.println(ningbingCode+"——————停止喷洒");
        // ningbingCode = "AB250203";
        /*
         * CollectorSocketServer c = new CollectorSocketServer(); c.poll3(ningbingCode);
         */
        if (HspUtil.map.containsKey(ningbingCode)
                && HspUtil.map.get(ningbingCode).getSocket() != null) {
            try {
                Socket ses = HspUtil.map.get(ningbingCode).getSocket();
                String sendMessage1 = "0000000000060005080BFF00";
                SocketUtil.sendData(sendMessage1, ses.getOutputStream());
                // 保存操作记录
                LhpRfpLogEntity rfpLog = new LhpRfpLogEntity();
                rfpLog.setPointCode(ningbingCode);
                rfpLog.setLogTime(new Date());
                rfpLog.setLogContent("发送指令，停止喷洒");
                lhpRfpLogService.save(rfpLog);
                Thread.sleep(1000);
                String sendMessage2 = "0000000000060005080B0000";
                SocketUtil.sendData(sendMessage2, ses.getOutputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @RequestMapping("/refreshWater/{pointCode}")
    public Map refreshWater(@PathVariable("pointCode") String pointCode) {
        Map map = new HashMap();
        String yewei = "";
        String status = "";
        String ddfK = "";
        String ddfG = "";
        String bpqStatus = "";
        String systemStatus = "";
        String ddfKdw = "";
        String ddfGdw = "";
        String ptNum = "";
        String yeya = "";

        String pCode1 = "AB" + pointCode.substring(2);
        Map<String, String> mapStr = HspUtil.eptMap1.get(pCode1);
        if (mapStr != null && mapStr.get("yewei") != null) {
            yewei = mapStr.get("yewei");
        }
        if (mapStr != null && mapStr.get("status") != null && mapStr.get("status").equals("0000")) {
            if(mapStr.get("status").equals("0000")) {
                status = "运行正常";
            }else {
                status = "系统异常";
            }
        }
        if (mapStr != null && mapStr.get("ddfK") != null) {
            ddfK = mapStr.get("ddfK");
        }
        if (mapStr != null && mapStr.get("ddfG") != null) {
            ddfG = mapStr.get("ddfG");
        }
        if (mapStr != null && mapStr.get("bpqStatus") != null) {
            bpqStatus = mapStr.get("bpqStatus");
        }
        if (mapStr != null && mapStr.get("systemStatus") != null) {
            systemStatus = mapStr.get("systemStatus");
        }
        if (mapStr != null && mapStr.get("ddfKdw") != null) {
            ddfKdw = mapStr.get("ddfKdw");
        }
        if (mapStr != null && mapStr.get("ddfGdw") != null) {
            ddfGdw = mapStr.get("ddfGdw");
        }
        if (mapStr != null && mapStr.get("ptNum") != null) {
            ptNum = mapStr.get("ptNum");
        }
        if (mapStr != null && mapStr.get("yeya") != null) {
            yeya = mapStr.get("yeya");
        }

        map.put("status", status);
        map.put("yewei", yewei);
        map.put("ddfK", ddfK);
        map.put("ddfG", ddfG);
        map.put("bpqStatus", bpqStatus);
        map.put("systemStatus", systemStatus);
        map.put("ddfKdw", ddfKdw);
        map.put("ddfGdw", ddfGdw);
        map.put("ptNum", ptNum);
        map.put("yeya", yeya);

        chaxunAuto(pointCode);
        return map;
    }

    public void chaxunAuto(String pointCode) {
        String ningbingCode = "AB" + pointCode.substring(2);
        // ningbingCode = "AB250203";
        /*
         * CollectorSocketServer c = new CollectorSocketServer(); c.poll1(ningbingCode);
         */
        if (HspUtil.map.containsKey(ningbingCode)
                && HspUtil.map.get(ningbingCode).getSocket() != null) {
            try {
                Socket ses = HspUtil.map.get(ningbingCode).getSocket();
                //读取液位
                String sendMessage10 = "000000000006000310640001";
                SocketUtil.sendData(sendMessage10, ses.getOutputStream());
                Thread.sleep(200);
                //读取设备状态
                String sendMessage3 = "0000000000060003104A0002";
                SocketUtil.sendData(sendMessage3, ses.getOutputStream());
                Thread.sleep(200);
                //读取Y值
                String sendMessage4 = "000000000006000205000020";
                SocketUtil.sendData(sendMessage4, ses.getOutputStream());
                Thread.sleep(200);
                //读取X值
                String sendMessage5 = "000000000006000204000010";
                SocketUtil.sendData(sendMessage5, ses.getOutputStream());
                Thread.sleep(200);
                //读取液压和喷头号
                String sendMessage6 = "0000000000060003102C0009";
                SocketUtil.sendData(sendMessage6, ses.getOutputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    // 查询连接状态
    @RequestMapping("/getSocketStatus/{pointCode}")
    public R getSocketStatus(@PathVariable("pointCode") String pointCode) {
        String status="未连接";
        if (HspUtil.map.containsKey(pointCode) && HspUtil.map.get(pointCode).getSocket() != null) {
            status="已连接";
        }
        return R.ok().put("status",status);
    }
    //关闭连接
    @RequestMapping("/closeSocket/{pointCode}")
    public R closeSocket(@PathVariable("pointCode") String pointCode) {
        if (HspUtil.map.containsKey(pointCode)) {
            Socket clientSocket = HspUtil.map.get(pointCode).getSocket();
            if(clientSocket != null){
                try{
                    clientSocket.close();
                    System.out.println("关闭"+pointCode+"链接！");
                }catch(Exception e){

                }
            }
        }
        return R.ok();
    }

}
